package src;

import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;


public class IDF {

	private Set<Integer> DocSet=new HashSet<Integer>();
	
	private int NO_OF_DOCS;
	private String FIELD_NAME="question";
	private int N=5;
	
    private IndexReader reader;
    private IndexSearcher searcher;
    
	IDF(IndexReader r,IndexSearcher s)
	{
		this.reader=r;
	    this.NO_OF_DOCS=reader.numDocs();
	    this.searcher=s;
	    
	}
	double getIDF(String engWord) throws Exception
	{
	List<Token> transOptions=TokenFetcher.getTokenList2(engWord);
	if(transOptions==null)
		return 0;
	Iterator i1=transOptions.iterator();
	TermQuery query;
	TopDocs results ;
	ScoreDoc[] hits ;
    Hits docIds;
	int i=0;
	while(i<N && i1.hasNext())
	   {
		query=new TermQuery(new Term(this.FIELD_NAME,((Token)i1.next()).getWord()));
		//Debug.printUTF(query.toString());
		docIds=searcher.search(query);
	    if(docIds.length()<1)
	    	return 0;
	    for(int j=0;j<docIds.length();j++)
	    	this.DocSet.add(new Integer(docIds.id(j)));
		//results=searcher.search(query,Integer.MAX_VALUE-1);
		//if(results.totalHits<1)
			//return 0;
		//hits = results.scoreDocs;
		/*for(int j=0;j<hits.length;j++)
			this.DocSet.add(new Integer(hits[j].doc));
			*/
		i++;
	}
	return Math.log(((double)this.NO_OF_DOCS/this.DocSet.size()));
	}
	
	public void destory() throws Exception
	{
		this.reader.close();
		this.searcher.close();
	}
}
